Welcome back.
So let's go over some questions that use dictionary accumulation.
In this question we're provided a string, sentence.
And the question asks us to split the string into a list of words, and then to
create a dictionary that contains each word and the number of times it occurs.
And that dictionary should be named word_counts.
So the first thing that I'm going to do
is I'm going to split our sentence into words.
We can do that by calling the split method on string.
I'll say words = sentence.split().
And so words is going to be a list of strings.
The first item is going to be The.
The 2nd item is going to be dog.
The 3rd item is going to be chased, and so on.
So then we want to use dictionary accumulation.
And in this question we’re told to use the dictionary word_counts.
So I’m going to name my accumulator variable word_counts.
And I’m going to initialize it as an empty dictionary.
Then, we need to loop through all of the words.
So I'll say for every word in words.
And we want to say, if that word isn't in our dictionary so
far, so if word is not in word_counts.
And if it's not there we initialize its value to 0,
so word_counts[word] = 0.
And then still inside of our for loop, but outside of the if statement,
we're going to increment the value of word_counts[word].
So word_counts[word] = word_counts[word] + 1.
So let's run our code, and we can see that it produces the correct value.
If I actually print out the value of word_counts.
So if I say print(word_counts).
Then we can see that the word 'The' with a capital T appeared one time.
The word 'the' with a lowercase t appeared three times.
'Into' appeared once.
'Rabbit' appeared twice, and so on.
In this question we're asked to create a dictionary called char_d from
the string stri, so that the key is a character and
its value is how many times that character occurred.
So this is again a straightforward application of the dictionary
accumulation pattern.
So I'm going to name my dictionary accumulator char_d.
Then I loop through every character in stri.
So I'll say for c in stri.
And if I haven't seen that character before,
so if c is not in char_d then char_d[c] = 0.
And then inside of the for, but outside of the if,
I say char_d[c] = its previous value.
+ 1.
So when I run my code then I can see that it worked correctly.
But let me run this code in CodeLens, just to see again what's going on here.
So on line 1 we initialize stri to be the string "what can I do".
On line 3 we initialize our accumulator variable, our accumulator dictionary,
to be an empty dictionary.
Then we loop through every character in stri.
So we first say, c is the character "w", or the first character.
And then we say if c is not in char_d, which it is not, so
we assign char_d[c] or char_d[w] to be 0, and then we increment it.
Same thing with the letter "h".
So "h" isn't in our dictionary, so we're going to add it, and
initialize its value to 1.
Same thing with "a".
The character "a" was not in our dictionary so we initialize it to 1.
Same thing with "t".
Same thing with the character " ".
And "c".
So at this point we're at this "a".
And we're going to see that "a" is in our dictionary.
We're going to skip this if, and
we're going to say char_d[a] = its previous value + 1.
So we'll see this update to be 2.
And then we keep adding characters as we go on.
The second time we get to a space.
So at this point we're at this space.
Then we'll see this value increment by 1, and so on.
That's all for now, until next time.